package com.googlecode.afdbaccess.web.portal.webcontent.edit_row.edit_row_form_data;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.jboss.resteasy.annotations.Form;

import com.googlecode.afdbaccess.cache.preparedstatement.PreparedStatementArgumentsSetterHelper;
import com.googlecode.afdbaccess.config.injectconfig.ManagedRequestTransaction;
import com.googlecode.afdbaccess.web.common.interceptors.DatabaseAccessing;
import com.googlecode.afdbaccess.web.common.interceptors.RequestLogged;
import com.googlecode.afdbaccess.web.common.json.ExtJsJsonResponse;

@RolesAllowed("theRole")
@RequestLogged
@DatabaseAccessing
@Path("edit-row/edit-row-form-data")
public class EditRowPutRest {
    @Inject
    private ManagedRequestTransaction t;

    @PUT
    @Path("{id}")
    @Produces("application/json")
    public ExtJsJsonResponse update(@SuppressWarnings("unused") @Context HttpServletRequest request, @PathParam("id") long id, @Form FormValues formValues) throws Exception {
        formValues.checkParams();
        
        String sql =
            "UPDATE webTable\n" +
            "SET\n" +
            "  stringAttribute=?\n" +
            "WHERE\n" +
            "  webTable_id=?\n";

        PreparedStatementArgumentsSetterHelper setter = new PreparedStatementArgumentsSetterHelper();
        setter.addArgument(formValues.stringAttribute);
        setter.addArgument(id);

        t.executeUpdate(sql, setter);
        
        return new ExtJsJsonResponse();
    }
}
